﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Collections.Generic;
using System.Text;


using System.Text;
using System.Diagnostics;
using System.Net.Sockets;

namespace Qplus.Infrastructure
{
    public class HttpTaskBack
    {

        public void httpPost()
        {

            try
            {

                //请求地址

                String url = "http://gotye.com.cn/CheckUpdate";

                //创建WebRequest类

                HttpWebRequest request = HttpWebRequest.CreateHttp(new Uri(url));



                //设置请求方式GET POST

                request.Method = "POST";



                //返回应答请求异步操作的状态

                request.BeginGetRequestStream(requestCallback, request);

            }

            catch (WebException e)
            {

                Debug.WriteLine("网络相关异常处理");//网络相关异常处理

            }

            catch (Exception e)
            {

                Debug.WriteLine("异常处理");//异常处理

            }

        }


        private void requestCallback(IAsyncResult result)
        {

            try
            {

                //获取异步操作返回的的信息

                HttpWebRequest request = (HttpWebRequest)result.AsyncState;

                //结束对 Internet 资源的异步请求

                StreamWriter postStream = new StreamWriter(request.EndGetRequestStream(result));

                postStream.WriteLine("UID=11111111");

                postStream.WriteLine("UKEY：asdfasdffdasdfasd");



                //返回应答请求异步操作的状态

                request.BeginGetResponse(responseCallback, request);

            }

            catch (WebException e)
            {

                //异常处理



            }

            catch (Exception e)
            {

                //异常处理



            }

        }

        private void responseCallback(IAsyncResult result)
        {

            try
            {

                //获取异步操作返回的的信息

                HttpWebRequest request = (HttpWebRequest)result.AsyncState;

                //结束对 Internet 资源的异步请求

                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);

                //解析应答头

                //parseRecvHeader(response.Headers);

                //获取请求体信息长度

                long contentLength = response.ContentLength;



                //获取应答码

                int statusCode = (int)response.StatusCode;

                string statusText = response.StatusDescription;



                //应答头信息验证

                using (Stream stream = response.GetResponseStream())
                {

                    //获取请求信息

                    StreamReader read = new StreamReader(stream);

                    string msg = read.ReadToEnd();

                    //Deployment.Current.Dispatcher.BeginInvoke(() =>
                    //{

                    //    textBlock1.Text = msg;

                    //});

                     Debug.WriteLine("msg");

                }



            }

            catch (WebException e)
            {

                //连接失败



            }

            catch (Exception e)
            {
                 Debug.WriteLine(e.Message);
               // e.Message



            }



        }

    }
}




//namespace Qplus.Infrastructure
//{
//    public partial class MainPage : PhoneApplicationPage
//    {
//        public MainPage()
//        {
//            InitializeComponent();
//        }
//        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
//        {
//            DoWebClient();
//            DoHttpWebRequest();
//        }
//        private void DoWebClient()
//        {
//            WebClient webClient = new WebClient();
//            webClient.OpenReadAsync(new Uri("http://www.cnblogs.com/linzheng"));//在不阻止调用线程的情况下，从资源返回数据
//            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);//异步操作完成时发生
//        }
//        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
//        {
//            using (StreamReader reader = new StreamReader(e.Result))
//            {
//                string contents = reader.ReadToEnd();
//                int begin = contents.ToString().IndexOf("<title>");
//                int end = contents.ToString().IndexOf("</title>");
//                string note = contents.Substring(contents.ToString().IndexOf("摘要"), 300);
//                webClientTextBlock.Text = contents.ToString().Substring(begin+7, end - begin-7);
//                textBox1.Text = note;
//            }

//        }
//        private void DoHttpWebRequest()
//        {
//            string url = "http://www.cnblogs.com/linzheng";
//            WebRequest request = HttpWebRequest.Create(url);//创建WebRequest类
//            IAsyncResult result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request);//返回异步操作的状态
//        }
//        private void ResponseCallback(IAsyncResult result)
//        {
//            HttpWebRequest request = (HttpWebRequest)result.AsyncState;//获取异步操作返回的的信息
//            WebResponse response = request.EndGetResponse(result);//结束对 Internet 资源的异步请求

//            using (Stream stream = response.GetResponseStream())
//            using (StreamReader reader = new StreamReader(stream))
//            {
//                string contents = reader.ReadToEnd();
//                int begin = contents.ToString().IndexOf("<title>");
//                int end = contents.ToString().IndexOf("</title>");
//                string note = contents.Substring(contents.ToString().IndexOf("摘要"), 300);
//                //通过呼叫UI Thread来改变页面的显示
//                Dispatcher.BeginInvoke(() => { httpWebRequestTextBlock.Text = contents.ToString().Substring(begin + 7, end - begin - 7); textBox2.Text = note; });

//            }
//        }
//    }
//}